/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author jefoutta
 */
public class Connection {
    private LogicGate logicGate1;
    private LogicGate logicGate2;
    private Source source;
    private int inputIndex;
    private char type;

    public int isCorrect(){
        if(type == 'g')
            if(logicGate1.getOutput() != logicGate2.getInput(inputIndex))
                return -2;
        if(type == 's')
            if(source.val != logicGate2.getInput(inputIndex))
                return -2;
        return logicGate2.getInput(inputIndex);
    }

    public boolean conect(LogicGate gateOutput, LogicGate gateInput){
        
        logicGate1 = gateOutput;
        logicGate1.setOutputChannel(this);
        logicGate2 = gateInput;
        inputIndex = logicGate2.getAvailableInputIndex();
        logicGate2.setInputChannel(this);
        type = 'g';
        return true;
    }

    public boolean conect(Source sourceId, LogicGate gateInput){
        
        source = sourceId;
        source.setOutputChannel(this);
        logicGate2 = gateInput;
        inputIndex = logicGate2.getAvailableInputIndex();
        logicGate2.setInputChannel(this);
        type = 's';
        return true;
    }

    public char getType (){
        return type;
    }

    public int getInputIndex(){
        return inputIndex;
    }

    public LogicGate getSourceGate(){
        return logicGate1;
    }

    public LogicGate getDestinyGate(){
        return logicGate2;
    }

    public Source getSource(){
        return source;
    }
}
